﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import Engine.Helpers.*;
    import __AS3__.vec.*;
    import Classes.bonus.*;

    public class ItemBonus {

        public static const COIN_YIELD:String = "coinYield";
        public static const XP_YIELD:String = "XP";
        public static const GOODS_YIELD:String = "goods";
        public static const PREMIUM_GOODS_YIELD:String = "premium_goods";
        public static const ALL:String = "all";
        private static const NEARBY:Number = 5;

        private static var m_curObject:ItemInstance;

        private var m_field:String;
        protected var m_percentModifier:Number = 0;
        private var m_radius:Number = 0;
        private var m_allowedTypes:Array;
        private var m_name:String;

        public function ItemBonus(_arg1:XML){
            var _local3:XML;
            var _local4:String;
            this.m_allowedTypes = new Array();
            super();
            this.m_field = _arg1.@field;
            this.m_percentModifier = _arg1.@percentModifier;
            this.m_radius = _arg1.@radius;
            this.m_name = _arg1.@name;
            var _local2:int;
            while (_local2 < _arg1.allowedType.length()) {
                _local3 = _arg1.allowedType[_local2];
                _local4 = _local3.@type;
                this.m_allowedTypes.push(_local4);
                _local2++;
            };
        }
        public static function getBonusModifier(_arg1:ItemInstance, _arg2:String):Number{
            var _local3:Array = new Array();
            var _local4:Array = ItemBonus.getBonuses(_arg1, _local3);
            var _local5:Number = 1;
            var _local6:int;
            while (_local6 < _local4.length) {
                if ((((((_local3[_local6] is Headquarter)) && ((_arg1 is Business)))) && ((_arg2 == COIN_YIELD)))){
                    _local5 = (_local5 + (((_local4[_local6] as ItemBonus).coinModifier * (_local3[_local6] as Headquarter).floorCount) / 100));
                } else {
                    if (((((_local4[_local6] as ItemBonus).field == _arg2)) || ((_arg2 == ALL)))){
                        _local5 = (_local5 + ((_local4[_local6] as ItemBonus).percentModifier / 100));
                    };
                };
                _local6++;
            };
            if (_local5 < 0){
                _local5 = 0;
            };
            return (_local5);
        }
        public static function getMaxBonusModifier(_arg1:ItemInstance, _arg2:String):Number{
            var _local3:Array = new Array();
            var _local4:Array = ItemBonus.getBonuses(_arg1, _local3);
            var _local5:Number = 1;
            var _local6:int;
            while (_local6 < _local4.length) {
                if ((((((_local3[_local6] is Headquarter)) && ((_arg1 is Business)))) && ((_arg2 == COIN_YIELD)))){
                    _local5 = (_local5 + (((_local4[_local6] as ItemBonus).coinModifier * (_local3[_local6] as Headquarter).floorCount) / 100));
                } else {
                    if (((((_local4[_local6] as ItemBonus).field == _arg2)) || ((_arg2 == ALL)))){
                        _local5 = (_local5 + ((_local4[_local6] as ItemBonus).maxPercentModifier / 100));
                    };
                };
                _local6++;
            };
            if (_local5 < 0){
                _local5 = 0;
            };
            return (_local5);
        }
        public static function getBonuses(_arg1:ItemInstance, _arg2:Array=null):Array{
            var _local5:ItemInstance;
            var _local6:Item;
            var _local7:ItemBonus;
            m_curObject = _arg1;
            var _local3:Array = Global.world.getObjectsByPredicate(matchBonus);
            var _local4:Array = new Array();
            if (_arg2 != null){
                _arg2.lenth = 0;
            };
            for each (_local5 in _local3) {
                if ((_local5 is ConstructionSite)){
                    _local6 = (_local5 as ConstructionSite).targetItem;
                } else {
                    _local6 = _local5.getItem();
                };
                for each (_local7 in _local6.bonuses) {
                    if (!_local7.bonusAppliesToObject(m_curObject)){
                    } else {
                        if (_local7.withinRadius(_local5, m_curObject)){
                            _local4.push(_local7);
                            if (_arg2 != null){
                                _arg2.push(_local5);
                            };
                        };
                    };
                };
            };
            _local4 = _local4.concat(getHarvestBonuses(_arg1));
            return (_local4);
        }
        private static function getHarvestBonuses(_arg1:ItemInstance):Array{
            var _local4:Vector.<HarvestBonus>;
            var _local5:HarvestBonus;
            var _local2:Array = [];
            var _local3:MapResource = (_arg1 as MapResource);
            if (_local3){
                _local4 = _local3.harvestingDefinition.harvestBonuses;
                for each (_local5 in _local4) {
                    _local5.init(_local3);
                    _local2.push(_local5);
                };
            };
            return (_local2);
        }
        public static function getNearbyBonuses(_arg1:ItemInstance, _arg2:Array=null):Array{
            var _local5:ItemInstance;
            var _local6:ItemBonus;
            m_curObject = _arg1;
            var _local3:Array = Global.world.getObjectsByPredicate(matchNearbyBonus);
            var _local4:Array = new Array();
            if (_arg2 != null){
                _arg2.lenth = 0;
            };
            for each (_local5 in _local3) {
                for each (_local6 in _local5.getItem().bonuses) {
                    if (!_local6.bonusAppliesToObject(m_curObject)){
                    } else {
                        if (_local6.withinNearbyRadius(_local5, m_curObject, NEARBY)){
                            _local4.push(_local6);
                            if (_arg2 != null){
                                _arg2.push(_local5);
                            };
                        };
                    };
                };
            };
            return (_local4);
        }
        public static function calcFranchiseBonuses(_arg1:Business):Number{
            var _local2:Array = new Array();
            var _local3:Array = ItemBonus.getBonuses(_arg1, _local2);
            var _local4:Number = 0;
            var _local5:int;
            while (_local5 < _local3.length) {
                if ((_local2[_local5] is Headquarter)){
                    _local4 = (_local4 + ((_local3[_local5] as ItemBonus).coinModifier * (_local2[_local5] as Headquarter).builtFloorCount));
                };
                _local5++;
            };
            return (_local4);
        }
        public static function calcIndividualHQBonus(_arg1:Business, _arg2:MapResource):Number{
            var _local7:MapResource;
            var _local8:Boolean;
            var _local3:Array = new Array();
            var _local4:Array = ItemBonus.getBonuses(_arg1, _local3);
            var _local5:Number = 0;
            var _local6:int;
            while (_local6 < _local4.length) {
                _local7 = _local3[_local6];
                if (_local7){
                    _local8 = (((_local7 is Headquarter)) || ((((_local7 is ConstructionSite)) && (((_local7 as ConstructionSite).targetItem.className == "Headquarter")))));
                    if (((_local8) && ((_local7.getId() == _arg2.getId())))){
                        _local5 = (_local5 + ((_local4[_local6] as ItemBonus).coinModifier * ((_arg2 is ConstructionSite)) ? 1 : (_local3[_local6] as Headquarter).builtFloorCount));
                        break;
                    };
                };
                _local6++;
            };
            return (_local5);
        }
        private static function matchBonus(_arg1:GameObject):Boolean{
            var _local3:Item;
            var _local4:ItemBonus;
            if ((!(_arg1) is MapResource)){
                return (false);
            };
            var _local2:MapResource = MapResource(_arg1);
            if (m_curObject.getId() == _local2.getId()){
                return (false);
            };
            if ((_local2 is ConstructionSite)){
                _local3 = (_local2 as ConstructionSite).targetItem;
            } else {
                _local3 = _local2.getItem();
            };
            for each (_local4 in _local3.bonuses) {
                if (!_local4.bonusAppliesToObject(m_curObject)){
                } else {
                    if (_local4.withinRadius(_local2, m_curObject)){
                        return (true);
                    };
                };
            };
            return (false);
        }
        private static function matchNearbyBonus(_arg1:GameObject):Boolean{
            var _local3:ItemBonus;
            if ((!(_arg1) is MapResource)){
                return (false);
            };
            var _local2:MapResource = MapResource(_arg1);
            if (m_curObject.getId() == _local2.getId()){
                return (false);
            };
            for each (_local3 in _local2.getItem().bonuses) {
                if (!_local3.bonusAppliesToObject(m_curObject)){
                } else {
                    if (_local3.withinNearbyRadius(_local2, m_curObject, NEARBY)){
                        return (true);
                    };
                };
            };
            return (false);
        }

        public function get name():String{
            return (this.m_name);
        }
        public function get field():String{
            return (this.m_field);
        }
        public function get percentModifier():Number{
            return (this.m_percentModifier);
        }
        public function get allowedTypes():Array{
            return (this.m_allowedTypes);
        }
        public function get maxPercentModifier():Number{
            return (this.percentModifier);
        }
        public function get radius():Number{
            return (this.m_radius);
        }
        public function get coinModifier():Number{
            var _local1:Number = 0;
            if (this.m_field == "coinYield"){
                _local1 = this.m_percentModifier;
            };
            return (_local1);
        }
        public function withinRadius(_arg1:ItemInstance, _arg2:ItemInstance):Boolean{
            return (this.withinNearbyRadius(_arg1, _arg2, 0));
        }
        private function withinNearbyRadius(_arg1:ItemInstance, _arg2:ItemInstance, _arg3:Number):Boolean{
            var _local4:Vector3 = _arg1.getSize();
            var _local5:Number = _local4.x;
            var _local6:Number = _local4.y;
            var _local7:Vector3 = _arg1.getPosition();
            var _local8:Number = (_local7.x + (_local5 * 0.5));
            var _local9:Number = (_local7.y + (_local6 * 0.5));
            _local4 = _arg2.getSize();
            var _local10:Number = _local4.x;
            var _local11:Number = _local4.y;
            _local7 = _arg2.getPosition();
            var _local12:Number = (_local7.x + (_local10 * 0.5));
            var _local13:Number = (_local7.y + (_local11 * 0.5));
            var _local14:Number = (_local8 - _local12);
            var _local15:Number = (_local9 - _local13);
            var _local16:Number = (this.m_radius + ((_local5 + _local10) / 2));
            var _local17:Number = (this.m_radius + ((_local6 + _local11) / 2));
            if ((((Math.abs(_local14) < _local16)) && ((Math.abs(_local15) < _local17)))){
                return (true);
            };
            return (false);
        }
        public function isNegative():Boolean{
            if (this.m_percentModifier < 0){
                return (true);
            };
            return (false);
        }
        public function effectiveRadius(_arg1:ItemInstance):Number{
            var _local2:Number = _arg1.getItem().sizeX;
            var _local3:Number = _arg1.getItem().sizeY;
            var _local4:Number = (Math.min(_local2, _local3) * 0.5);
            return ((this.m_radius + _local4));
        }
        public function bonusAppliesToObject(_arg1:ItemInstance):Boolean{
            var _local5:String;
            if ((_arg1 is LotSite)){
                return (false);
            };
            var _local2:Item = _arg1.getItem();
            var _local3:String = _local2.name;
            var _local4:String = _local2.type;
            for each (_local5 in this.m_allowedTypes) {
                if (_local5 == _local4){
                    return (true);
                };
            };
            return (false);
        }

    }
}//package Classes 
